<?php
/**
 *
 *   User:King
 *   Date:2021/12/9 17:05
 */

namespace app\bank_api\controller;


use app\admin\model\Admin;
use app\common\library\Integrate;
use app\common\model\Branch;
use app\common\model\Business;
use app\common\model\NewVoucher as NewVoucherModel;
use app\common\model\NewOrder;
use app\common\model\VoucherAccount;
use think\Db;
use think\Exception;
use think\Log;

class NewVoucher extends Base
{
    protected $noNeedRight = '*';

    public function list()
    {
        $staff     = $this->staffinfo;
        $user_money = $this->request->post('user_money');
        $category  = $this->request->post('category');

        $where = [
            'status'=>1,
            'admin_id'=>$staff['admin_id'],
        ];

        if(!empty($category)){
            $where['type'] = $category;
        }


        $is_limit_voucher = Admin::where('id',$staff['admin_id'])->value('is_limit_voucher');


        if(!empty($user_money) && $is_limit_voucher == 1){
            if($staff['admin_id'] != 37){
                $where['limit_min'] = ['<',$user_money];
                $where['limit_max'] = ['>=',$user_money];
            }else{
                $where['limit_max'] = ['<=',$user_money];
            }
        }

        $list =  NewVoucherModel::field('id,name,money_hui')->where($where)->order('type asc,id desc')->select();
        $this->success('成功',$list);
    }
    
    
    
    public function check()
    {
        $staff     = $this->staffinfo;
        $post_data = $this->request->post();

        if($staff['admin_id'] == 35) $this->error('活动已结束');
        $business   = Business::where(['id'=>$post_data['business_id'],'admin_id'=>$staff['admin_id']])->find();
        if(!$business){
            $this->error('参数错误');
        }

        //新郑要求每个用户号领取一次
        if($staff['admin_id'] == 38){
            $ex_count = NewOrder::where('business_id',$post_data['business_id'])
                ->where('admin_id',$staff['admin_id'])
                ->where('room_no',$post_data['room_no'])
                ->where('status','>',1)
                ->count();
            if($ex_count > 0){
                $this->error('该暖气用户号已领取');
            }
        }

        $this->success();
    }


    public function addVoucher()
    {
        $staff     = $this->staffinfo;
        $post_data = $this->request->post();

        if($staff['admin_id'] == 35) $this->error('活动已结束');
        $business   = Business::where(['id'=>$post_data['business_id'],'admin_id'=>$staff['admin_id']])->find();

        //如果有手机号，判断手机号是否提交过
        if(!empty($post_data['user_phone']))
        {
            $ex_count = NewOrder::where('business_id',$post_data['business_id'])->where('admin_id',$staff['admin_id'])->where('user_phone',$post_data['user_phone'])->whereTime('createtime','today')->where('status','>=',2)->count();
            if($ex_count > 0){
                $this->error('该手机号用户当天已参与过该活动');
            }
        }
        
        //新郑要求每个用户号领取一次
        if($staff['admin_id'] == 38){
            $ex_count = NewOrder::where('business_id',$post_data['business_id'])
                ->where('admin_id',$staff['admin_id'])
                ->where('room_no',$post_data['room_no'])
                ->where('status','>',1)
                ->count();
            if($ex_count > 0){
                $this->error('每个暖气用户号只能领取一次');
            }
        }


        $voucher_data = $post_data['voucher'];
        $template_ids = array_column($voucher_data,'template_id');

        $template_array = array_reduce($voucher_data, function ($carry, $item) {
            $carry[$item['template_id']] = $item['number'];
            return $carry;
        }, []);

        foreach($template_array as $value)
        {
            if($value > 10){
                $this->error('单个优惠券最多发放十张优惠券');
            }
        }

      
        $newVouchers = NewVoucherModel::whereIn('id',$template_ids)->where('admin_id',$staff['admin_id'])->where('status',1)->select();
        
        $error = 0;
        $error_mes = '';
        $total_money = 0;
        foreach ($newVouchers as $newVoucher)
        {
            if($newVoucher->sy_number < $template_array[$newVoucher->id]){
                $error = 1;
                $error_mes .= $newVoucher->name.',';
            }
            $total_money += $newVoucher->money_hui;
        }
        if($error == 1){
            $this->error($error_mes.'库存数量不足,请联系管理员添加');
        }
        //员工限额
        $staff_limit = \app\common\model\Staff::where('id',$staff['id'])->value('limit_money');
        if($staff_limit > 0){
            $money =  NewOrder::where('staff_id',$staff['id'])->where('type',1)->where('status','<>',1)->sum('money');
            if(($money + $total_money) > $staff_limit){
                $this->error('您的立减券额度已不足，请联系管理员');
            }
        }

        //支行限额
        $branch_limit = Branch::where('id',$staff['branch_id'])->value('limit_money');
        if($branch_limit > 0){
            $staff_ids = \app\common\model\Staff::where('branch_id',$staff['branch_id'])->column('id');
            $money =  NewOrder::whereIn('staff_id',$staff_ids)->where('type',1)->where('status','<>',1)->sum('money');
            if(($money + $total_money) > $branch_limit){
                $this->error('支行立减券额度已不足，请联系管理员');
            }
        }


        if(!$business){
            $this->error('参数错误');
        }

        $branch_name = Branch::where('id',$staff['branch_id'])->value('name');

        Db::startTrans();
        try{
            $data = [];
            $order_no = time().rand(1000,9999);
            foreach ($newVouchers as $newVoucher){
                for($i = 1; $i <= $template_array[$newVoucher->id]; $i++){
                    $arr = [
                        'order_no'=>$order_no,
                        'staff_id'=>$staff['id'],
                        'admin_id'=>$staff['admin_id'],
                        'user_name'=>$post_data['user_name']??'',
                        'user_phone'=>$post_data['user_phone']??'',
                        'user_money'=>$post_data['user_money']??'',
                        'business_id'=>$post_data['business_id'],
                        'voucher_id'=>$newVoucher->id,
                        'voucher_name'=> $newVoucher->name,
                        'type'=>$newVoucher->type,
                        'status'=>1,
                        'money'=> $newVoucher->money_hui,
                        'gyh'=>$post_data['gyh'] ?? '',
                        'time_limit'=> $post_data['time_limit_id'] ??'',
                        'branch_id'=>$staff['branch_id'],
                        'branch_name'=>$branch_name??'',
                        'business_name'=>$business->name??'',
                        'image'=>$post_data['image']??'',
                        'remark'=>$post_data['remark']??'',
                        'room_no' => $post_data['room_no']?? '',
                        'pay_date' => $post_data['pay_date']?? '',
                        'number'=>1,
                    ];
                    array_push($data,$arr);
                }

            }
            $order = new NewOrder();
            $order->saveAll($data);

            $bank_name = Admin::where('id',$staff['admin_id'])->value('nickname');
            $param = urlencode('order_no='.$order_no.'&bank_name='.$bank_name);
            $url = $this->request->domain().'/packet/?param='.$param;
            Db::commit();
            $this->success('成功',$url);
        }catch (Exception $e){
            Db::rollback();
            $this->error('失败');
        }

    }

    public function addWupin()
    {
        $staff     = $this->staffinfo;
        $post_data = $this->request->post();

        if($staff['admin_id'] == 35) $this->error('活动已结束');
        $business   = Business::where(['id'=>$post_data['business_id'],'admin_id'=>$staff['admin_id']])->find();

        //如果有手机号，判断手机号是否提交过
        if(!empty($post_data['user_phone']))
        {
            $ex_count = NewOrder::where('business_id',$post_data['business_id'])->where('admin_id',$staff['admin_id'])->where('user_phone',$post_data['user_phone'])->whereTime('createtime','today')->where('status','>=',2)->count();
            if($ex_count > 0){
                $this->error('该手机号用户当天已参与过该活动');
            }
        }


        $voucher_data = $post_data['voucher'];
        $template_ids = array_column($voucher_data,'template_id');

        $template_array = array_reduce($voucher_data, function ($carry, $item) {
            $carry[$item['template_id']] = $item['number'];
            return $carry;
        }, []);







        $newVouchers = NewVoucherModel::whereIn('id',$template_ids)->where('admin_id',$staff['admin_id'])->where('status',1)->select();
        $error = 0;
        $error_mes = '';
        $total_money = 0;
        foreach ($newVouchers as $newVoucher)
        {
            $account = VoucherAccount::where('branch_id',$staff['branch_id'])->where('voucher_id',$newVoucher->id)->find();
            $sy_number = $account->total_number - $account->send_number;
            if($sy_number < $template_array[$newVoucher->id]){
                $error = 1;
                $error_mes .= $newVoucher->name.',';
            }
            $total_money += $newVoucher->money_hui;
        }
        if($error == 1){
            $this->error($error_mes.'库存数量不足,请联系管理员添加');
        }

        if(!$business){
            $this->error('参数错误');
        }

        $branch_name = Branch::where('id',$staff['branch_id'])->value('name');

        Db::startTrans();
        try{
            $data = [];
            $order_no = time().rand(1000,9999);
            foreach ($newVouchers as $newVoucher){
                $arr = [
                    'order_no'=>$order_no,
                    'staff_id'=>$staff['id'],
                    'admin_id'=>$staff['admin_id'],
                    'user_name'=>$post_data['user_name']??'',
                    'user_phone'=>$post_data['user_phone']??'',
                    'user_money'=>$post_data['user_money']??'',
                    'business_id'=>$post_data['business_id'],
                    'voucher_id'=>$newVoucher->id,
                    'voucher_name'=> $newVoucher->name,
                    'type'=>$newVoucher->type,
                    'status'=>2,
                    'money'=> bcmul($newVoucher->money_hui,$template_array[$newVoucher->id],2),
                    'gyh'=>$post_data['gyh'] ?? '',
                    'time_limit'=> $post_data['time_limit_id'] ??'',
                    'branch_id'=>$staff['branch_id'],
                    'branch_name'=>$branch_name??'',
                    'business_name'=>$business->name??'',
                    'image'=>$post_data['image']??'',
                    'remark'=>$post_data['remark']??'',
                    'number'=>$template_array[$newVoucher->id],
                    'extend1'=>$post_data['extend1']??'1',
                ];
                array_push($data,$arr);

            }
            $order = new NewOrder();
            $order->saveAll($data);

            foreach ($newVouchers as $newVoucher){
                $account = VoucherAccount::where('branch_id',$staff['branch_id'])->where('voucher_id',$newVoucher->id)->find();
                $account->send_number += $template_array[$newVoucher->id];
                $account->save();
            }

            Db::commit();
            $this->success('成功',[]);
        }catch (Exception $e){
            Db::rollback();
            $this->error('失败');
        }

    }






    public function add()
    {
        $staff     = $this->staffinfo;
        $post_data = $this->request->post();
        if($staff['admin_id'] == 35) $this->error('活动已结束');
        $business   = Business::where(['id'=>$post_data['business_id'],'admin_id'=>$staff['admin_id']])->find();

       /* if($post_data['business_id'] == 15 ){
            if(time() < 1704038400) $this->error('活动还未开始');
            $ex_count = NewOrder::where('business_id',15)->where('admin_id',$staff['admin_id'])->where('user_phone',$post_data['user_phone'])->where('status',2)->count();
            if($ex_count > 0){
                $this->error('该手机号用户已发放立减券');
            }
        }*/


        //如果有手机号，判断手机号是否提交过
        if(!empty($post_data['user_phone']))
        {
            $ex_count = NewOrder::where('business_id',$post_data['business_id'])->where('admin_id',$staff['admin_id'])->where('user_phone',$post_data['user_phone'])->whereTime('createtime','today')->where('status','>=',2)->count();
            if($ex_count > 0){
                $this->error('该手机号用户当天已参与过该活动');
            }
        }

        //判断优惠券
        $where = [
            'status'=>1,
            'admin_id'=>$staff['admin_id']
        ];
        if($post_data['category'] == 1){
            $is_limit_voucher = Admin::where('id',$staff['admin_id'])->value('is_limit_voucher');
            if(!empty($post_data['user_money']) && $is_limit_voucher == 1){
                $where['limit_min'] = ['<',$post_data['user_money']];
                $where['limit_max'] = ['>=',$post_data['user_money']];
            }
            $voucher_ids = NewVoucherModel::where($where)->column('id');

            if(!in_array($post_data['template_id'],$voucher_ids)){
                $this->error('请选择正确的立减券');
            }
            //限制发放立减券数量
            if($is_limit_voucher == 1){
                $voucher1 =  NewVoucherModel::where('id',$post_data['template_id'])->where('admin_id',$staff['admin_id'])->where('status',1)->find();
                $limit_number = intval($voucher1->limit_max);
                if($post_data['number'] > $limit_number){
                    $this->error('该业务金额最多只能发放'.$limit_number.'张券');
                }

            }
        }



        DB::startTrans();
        $newVoucher = NewVoucherModel::where('id',$post_data['template_id'])->where('admin_id',$staff['admin_id'])->where('status',1)->lock(true)->find();

        if($post_data['category'] == 1){//立减券
            //员工限额
            $staff_limit = \app\common\model\Staff::where('id',$staff['id'])->value('limit_money');
            if($staff_limit > 0){
                $money =  NewOrder::where('staff_id',$staff['id'])->where('type',1)->where('status','<>',1)->sum('money');
                if(($money + $newVoucher['money_hui']) > $staff_limit){
                    Db::rollback();
                    $this->error('您的立减券额度已不足，请联系管理员');
                }
            }

            //支行限额
            $branch_limit = Branch::where('id',$staff['branch_id'])->value('limit_money');
            if($branch_limit > 0){
                $staff_ids = \app\common\model\Staff::where('branch_id',$staff['branch_id'])->column('id');
                $money =  NewOrder::whereIn('staff_id',$staff_ids)->where('type',1)->where('status','<>',1)->sum('money');
                if(($money + $newVoucher['money_hui']) > $branch_limit){
                    Db::rollback();
                    $this->error('支行立减券额度已不足，请联系管理员');
                }
            }
            if($newVoucher['sy_number'] < 1){
                Db::rollback();
                $this->error('该优惠券数量已用完');
            }
        }else{//实物
            //支行限制
            $account = VoucherAccount::where('branch_id',$staff['branch_id'])->where('voucher_id',$newVoucher->id)->find();
            $sy_number = $account->total_number - $account->send_number;


            if($account && ($sy_number < (int)$post_data['number'])){
                Db::rollback();
                $this->error('该商品剩余数量不足');
            }

        }


        if(!$business || !$newVoucher){
            Db::rollback();
            $this->error('参数错误');
        }



        $branch_name = Branch::where('id',$staff['branch_id'])->value('name');


        try{
            $order = new NewOrder();
            $order->order_no        = time().rand(1000,9999);
            $order->staff_id        = $staff['id'];
            $order->admin_id        = $staff['admin_id'];
            $order->user_name       = $post_data['user_name']??'';
            $order->user_phone      = $post_data['user_phone']??'';
            $order->user_money      = $post_data['user_money']??'';
            $order->business_id     = $post_data['business_id'];
            $order->voucher_id      = $post_data['template_id'];
            $order->voucher_name    = $newVoucher->name;
            $order->type            = $newVoucher->type;
            $order->status          = $post_data['category'] == 2 ? 2: 1;
            $order->money           = $post_data['category'] == 2 ? 0 : $newVoucher->money_hui;
            $order->gyh             = $post_data['gyh'] ?? '';
            $order->time_limit      = $post_data['time_limit_id'] ??'';
            $order->branch_id       = $staff['branch_id'];
            $order->branch_name     = $branch_name??'';
            $order->business_name   = $business->name??'';
            $order->image           = $post_data['image']??'';
            $order->remark          = $post_data['remark']??'';
            $order->number          = $post_data['number']?? 1;
            $order->room_no         = $post_data['room_no']?? '';
            $order->pay_date        = $post_data['pay_date']?? '';
            $order->save();

            /*if($post_data['category'] == 1){
                //优惠券剩余数量减少
                $newVoucher->sy_number -= $order->number;
                $newVoucher->save();
            }*/

            //如果为实物商品,减少支行库存
            if($post_data['category'] == 2){
                if($account){
                    $account->send_number += $order->number;
                    $account->save();
                }else{
                    $newVoucher->sy_number -= $order->number;
                    $newVoucher->save();
                }
            }

            $bank_name = Admin::where('id',$staff['admin_id'])->value('nickname');
            $param = urlencode('order_no='.$order->order_no.'&bank_name='.$bank_name);
            $url = 'https://yingxiao.nongxinhui.com/packet/?param='.$param;
            Db::commit();
            $this->success('成功',$url);
        }catch (Exception $e){
            Db::rollback();
            $this->error('失败');
        }

    }

    public function add2()
    {
        $staff     = $this->staffinfo;
        $post_data = $this->request->post();
        if($staff['admin_id'] == 35) $this->error('活动已结束');
        $business   = Business::where(['id'=>$post_data['business_id'],'admin_id'=>$staff['admin_id']])->find();

        if($post_data['business_id'] == 15 ){
            if(time() < 1704038400) $this->error('活动还未开始');
            $ex_count = NewOrder::where('business_id',15)->where('admin_id',$staff['admin_id'])->where('user_phone',$post_data['user_phone'])->where('status',2)->count();
            if($ex_count > 0){
                $this->error('该手机号用户已发放立减券');
            }
        }
        //判断优惠券
        $where = [
            'status'=>1,
            'admin_id'=>$staff['admin_id']
        ];
        if($post_data['category'] == 1){
            $is_limit_voucher = Admin::where('id',$staff['admin_id'])->value('is_limit_voucher');
            if(!empty($post_data['user_money']) && $is_limit_voucher == 1){
                $where['limit_min'] = ['<=',$post_data['user_money']];
                $where['limit_max'] = ['>',$post_data['user_money']];
                $where['sy_number'] = ['>',0];
            }
            $voucher_ids = NewVoucherModel::where($where)->column('id');

            if(empty($voucher_ids)){
                $this->error('立减券名额已用完');
            }


          /*  if(!in_array($post_data['template_id'],$voucher_ids)){
                $this->error('请选择正确的立减券');
            }*/

            $post_data['template_id'] = $voucher_ids[array_rand($voucher_ids,1)];

        }
        DB::startTrans();
        $newVoucher = NewVoucherModel::where('id',$post_data['template_id'])->where('admin_id',$staff['admin_id'])->where('status',1)->lock(true)->find();

        if($post_data['category'] == 1){
            //员工限额
            $staff_limit = \app\common\model\Staff::where('id',$staff['id'])->value('limit_money');
            if($staff_limit > 0){
                $money =  NewOrder::where('staff_id',$staff['id'])->where('type',1)->where('status','<>',1)->sum('money');
                if(($money + $newVoucher['money_hui']) > $staff_limit){
                    Db::rollback();
                    $this->error('您的立减券额度已不足，请联系管理员');
                }
            }

            //支行限额
            $branch_limit = Branch::where('id',$staff['branch_id'])->value('limit_money');
            if($branch_limit > 0){
                $staff_ids = \app\common\model\Staff::where('branch_id',$staff['branch_id'])->column('id');
                $money =  NewOrder::whereIn('staff_id',$staff_ids)->where('type',1)->where('status','<>',1)->sum('money');
                if(($money + $newVoucher['money_hui']) > $branch_limit){
                    Db::rollback();
                    $this->error('支行立减券额度已不足，请联系管理员');
                }
            }
        }


        if(!$business || !$newVoucher){
            Db::rollback();
            $this->error('参数错误');
        }

        if($newVoucher['sy_number'] < 1){
            Db::rollback();
            $this->error('优惠券数量已用完,请更换其他优惠券');
        }

        /*if(empty($post_data['user_name']) || mb_strlen($post_data['user_name']) >=10)
        {
            Db::rollback();
            $this->error('请输入正确的客户姓名');
        }

        if(empty($post_data['user_phone']) || strlen($post_data['user_name']) >=11)
        {
            Db::rollback();
            $this->error('请输入正确的客户手机号');
        }

        if($post_data['user_money'] <= 0)
        {
            Db::rollback();
            $this->error('请输入正确的金额');
        }

        if(empty($post_data['image']))
        {
            Db::rollback();
            $this->error('请上传业务照片');
        }*/


        $branch_name = Branch::where('id',$staff['branch_id'])->value('name');


        try{
            $order = new NewOrder();
            $order->order_no        = time().rand(1000,9999);
            $order->staff_id        = $staff['id'];
            $order->admin_id        = $staff['admin_id'];
            $order->user_name       = $post_data['user_name']??'';
            $order->user_phone      = $post_data['user_phone']??'';
            $order->user_money      = $post_data['user_money']??'';
            $order->business_id     = $post_data['business_id'];
            $order->voucher_id      = $post_data['template_id'];
            $order->voucher_name    = $newVoucher->name;
            $order->type            = $newVoucher->type;
            $order->status          = $post_data['category'] == 2 ? 2: 1;
            $order->money           = $post_data['category'] == 2 ? 0 : $newVoucher->money_hui;
            $order->gyh             = $post_data['gyh'] ?? '';
            $order->time_limit      = $post_data['time_limit_id'] ??'';
            $order->branch_id       = $staff['branch_id'];
            $order->branch_name     = $branch_name??'';
            $order->business_name   = $business->name??'';
            $order->image           = $post_data['image']??'';
            $order->remark          = $post_data['remark']??'';
            $order->save();

            if($post_data['category'] == 2){
                $newVoucher->sy_number -= 1;
                $newVoucher->save();
            }

            $bank_name = Admin::where('id',$staff['admin_id'])->value('nickname');
            $param = urlencode('order_no='.$order->order_no.'&bank_name='.$bank_name);
            $url = 'https://yingxiao.nongxinhui.com/packet/?param='.$param;
            Db::commit();
            $this->success('成功',$url);
        }catch (Exception $e){
            Db::rollback();
            $this->error('失败');
        }

    }


    public function get_jifen()
    {
        $extend1    = $this->request->post('extend1');
        $time_limit = $this->request->post('time_limit');
        $user_money = $this->request->post('user_money');

        if(empty($extend1) || empty($time_limit) || empty($user_money)){
            $this->success('成功',['jifen'=>0]);
        }

        $arr = [
            1=>[
                '1'=>2.5,
                '2'=>5,
                '3'=>10,
                '4'=>10,
                '5'=>10,
            ],
            2=>[
                '1'=>4,
                '2'=>8,
                '3'=>15,
                '4'=>15,
                '5'=>15,
            ],
        ];

        if($extend1 == 1){
            $base = $arr[1][$time_limit] ?? 0;
        }else{
            $base = $arr[2][$time_limit] ?? 0;
        }



        /*if($user_money >= 20){
            $base = $arr[2][$time_limit] ?? 0;
        }else{
            if($extend1 == 2){
                $base = $arr[2][$time_limit] ?? 0;
            }else{
                $base = $arr[1][$time_limit] ?? 0;
            }
        }*/
        $jifen = bcmul($base,$user_money,1);
        $this->success('成功',['jifen'=>$jifen]);


    }



    public function timeLimit()
    {
        $staff     = $this->staffinfo;
        $data = [
            '0'=>[
                ['id'=>'活期','name'=>'活期'],
                ['id'=>'一天','name'=>'一天'],
                ['id'=>'7天','name'=>'7天'],
                ['id'=>'3个月','name'=>'3个月'],
                ['id'=>'6个月','name'=>'6个月'],
                ['id'=>'一年','name'=>'一年'],
            ],
            '28'=>[
                ['id'=>'活期','name'=>'活期'],
                ['id'=>'一个月','name'=>'一个月'],
                ['id'=>'二个月','name'=>'二个月'],
                ['id'=>'三个月','name'=>'三个月'],
                ['id'=>'四个月','name'=>'四个月'],
                ['id'=>'五个月','name'=>'五个月'],
                ['id'=>'六个月','name'=>'六个月'],
                ['id'=>'七个月','name'=>'七个月'],
                ['id'=>'八个月','name'=>'八个月'],
                ['id'=>'九个月','name'=>'九个月'],
                ['id'=>'十个月','name'=>'十个月'],
                ['id'=>'十一个月','name'=>'十一个月'],
                ['id'=>'十二个月','name'=>'十二个月'],
            ],
            '37'=>[
                ['id'=>'1','name'=>'三个月'],
                ['id'=>'2','name'=>'六个月'],
                ['id'=>'3','name'=>'一年'],
                ['id'=>'4','name'=>'二年'],
                ['id'=>'5','name'=>'三年'],

            ]
        ];

        $info = $data[$staff['admin_id']] ?? $data[0];
        $this->success('成功',$info);
    }

    public function business()
    {
        $id = $this->request->param('business_id');

        $info = Business::where('id',$id)->find();

        $this->success('成功',$info);
    }


    //获取商品库存
    public function get_stock()
    {
        $staff     = $this->staffinfo;
        $branch= Branch::where('id',$staff['branch_id'])->find();
        $voucher_account = VoucherAccount::where(['branch_id'=>$staff['branch_id']])
        ->with('voucher')
        ->select();
        //获取实物数据
        $return_data = [];
        $return_data['voucher'] = [
            'name' => '立减券',
            'sy' => 0,
            'send' => 0
        ];
        foreach($voucher_account as $item){
            if($item['voucher']['type'] == 2){
                $return_data['goods'][] = [
                    'name' => $item['voucher']['name'],
                    'total_number' => $item['total_number'],
                    'send' => $item['send_number'],
                    'sy' => $item['total_number'] - $item['send_number']
                ];
            }
        }
        //根据voucher_id分组
        $order_count = NewOrder::where(['branch_id'=>$staff['branch_id'],'type'=>1,'status'=>['>',1]])
        ->group('voucher_id')->column('voucher_id, sum(number) as count', 'voucher_id');
        foreach($order_count as $k=>$item){
            
            //获取券的面额
            $money = NewVoucherModel::where('id',$k)->where(['type'=>1,'status'=>1])->value('money_hui');
            $return_data['voucher']['send'] += $item * $money;
        }
        $return_data['voucher']['sy'] = $branch['limit_money'] - $return_data['voucher']['send'];
        $this->success('成功',$return_data);

    }
}
